using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using Poka_Yoke.Analysis;
using Configuration=Poka_Yoke.Analysis.Configuration;

namespace Poka_Yoke
{
    class Program
    {
        public static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                PrintHelp(ErrorMessages.IncorrectNumberOfArguments);
                return;
            }

            Configuration config = (Configuration) ConfigurationManager.GetSection("PokaYoke");

            Analyser programAnalyser = new Analyser(config);

            if (File.Exists(args[0]))
            {
                List<AnalysisResult> result = programAnalyser.AnalyseFile(args[0]);
                DisplayResult(result);
                return;
            }

            if (Directory.Exists(args[0]))
            {
                List<AnalysisResult>  result = programAnalyser.AnalyseDirectory(args[0]);
                DisplayResult(result);
                return;
            }

            PrintHelp(ErrorMessages.FileOrDirectoryNotFound);
        }

        private static void DisplayResult(List<AnalysisResult> results)
        {
            if (results == null)
            {
                throw new ArgumentNullException("results");
            }

            foreach (AnalysisResult currentResult in results)
            {
                Console.WriteLine("{0}\t{1}\t{2}", currentResult.LineNumber, currentResult.ErrorSeverity,
                                  currentResult.ErrorMessage);
            }
        }

        private static void PrintHelp(string errorMessage)
        {
            if (string.IsNullOrEmpty(errorMessage))
            {
                throw new ArgumentNullException("errorMessage");
            }

            Console.WriteLine(errorMessage);

            Console.WriteLine(ErrorMessages.StandardHelp);
        }
     }
}
